<template>
	<view>
		<uni-nav-bar title="发现" :rightText="positionAddress" @clickRight="clickLocal()"></uni-nav-bar>
		<uni-popup ref="popup" type="dialog" maskClick="ture">
		    <uni-popup-dialog mode="input" title="当前位置" placeholder="请输入当前位置" message="成功消息" :duration="2000" :before-close="true" @close="close" @confirm="confirm"></uni-popup-dialog>
		</uni-popup>
		<view v-for="hotel in hotels" @click="clickHotel(hotel)">
				<find_hotel :hotel_data=hotel></find_hotel>
		</view>
	</view>
</template>

<script>
	import find_hotel from '../../components/find_hotel.vue';
	import config from '../../common/config.js';
	import uniNavBar from "@/components/uni-nav-bar/uni-nav-bar.vue";
	import uniPopup from '@/components/uni-popup/uni-popup.vue';
	import uniPopupDialog from '@/components/uni-popup/uni-popup-dialog.vue';
	export default {
		data() {
			return {
				"hotels": [],
				"positionAddress":"苏州",
				"latitude": "31.299379",//维度
				"longitude": "120.619585",//经度
			}
		},
		methods: {
			hotelRequest() {
				uni.request({
					url: config.hosts+'hotel/list',
					method: 'POST',
					header:{
						'content-type': 'application/json'
					},
					data: {
						"latitude": this.latitude,
						"longitude": this.longitude
					},
					success: res => {
						this.hotels = res.data;
					},
					fail: () => {
						console.log("失败");
					},
					complete: () => {
					}
				});
			},
			addressPositionRequest() {
				uni.request({
					url: "https://restapi.amap.com/v3/config/district?key=8458d498113fec7c1ade1b6a15e398dc&output=JSON"+"&keywords="+this.positionAddress,
					method: 'GET',
					header:{
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
					},
					success: res => {
						const districts = res.data.districts[0];
						const center = districts.center;
						const array = center.split(",")
						this.longitude = array[0];
						this.latitude = array[1];
						this.hotelRequest();
					},
					fail: () => {
						console.log("失败");
					},
					complete: () => {
					}
				});
			},
			clickHotel(e) {
				var data = JSON.stringify(e);
				uni.navigateTo({
					url: '../hotelinfo/hotelinfo?data='+data,
					success: res => {},
					fail: () => {},
					complete: () => {}
				});
			},
			clickLocal() {
				this.$refs.popup.open()
			},
			confirm(done,value) {
				this.positionAddress = value
				this.addressPositionRequest()
				this.close()
			},
			close() {
				this.$refs.popup.close()
			}
		},
		onLoad() {
			this.hotelRequest();
			config.defaultUserInfo();
		},
		components: {
			find_hotel,uniNavBar,uniPopup,uniPopupDialog
		}
	}
</script>

<style>

</style>
